﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace He.Web.manage.attrfield
{
    public partial class list : He.Web.UI.Manage
    {
        public int channel_id;                                //栏目ID
        public Model.channel channel;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!int.TryParse(Request.Params["channel_id"] as string, out this.channel_id))
            {
                JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您要修改的信息不存在或参数不正确。", "back", "Error");
                return;
            }
            channel = new BLL.channel().GetModel(channel_id);

            if (!Page.IsPostBack)
            {
                chkLoginLevel("viewAttrfield" + channel_id + " order by sort_id asc");
                this.RptBind("channel_id=" + channel_id);
            }
        }

        #region 数据列表绑定
        private void RptBind(string strWhere)
        {
            PagingInfo.Current = this.GetInt("page", 0);
            PagingInfo.PageSize = 15;

            He.BLL.attr_field bll = new He.BLL.attr_field();
            //获得总条数
            PagingInfo.RecordCount = bll.GetRecordCount(strWhere);
            lbtnDel.Enabled = PagingInfo.RecordCount > 0;

            this.rptList.DataSource = bll.GetList(strWhere);
            this.rptList.DataBind();
        }
        #endregion

        #region 返回字段类型中文名称
        protected string GetTypeCn(string _control_type)
        {
            string type_name = "";
            switch (_control_type)
            {
                case "single-text":
                    type_name = "单行文本";
                    break;
                case "multi-text":
                    type_name = "多行文本";
                    break;
                case "editor":
                    type_name = "编辑器";
                    break;
                case "images":
                    type_name = "图片上传";
                    break;
                case "number":
                    type_name = "数字";
                    break;
                case "checkbox":
                    type_name = "复选框";
                    break;
                case "multi-radio":
                    type_name = "多项单选";
                    break;
                case "multi-checkbox":
                    type_name = "多项多选";
                    break;
                default:
                    type_name = "未知类型";
                    break;
            }
            return type_name;
        }
        #endregion

        //删除
        protected void lbtnDel_Click(object sender, EventArgs e)
        {
            chkLoginLevel("delAttrfield" + channel_id);
            BLL.attr_field bll = new BLL.attr_field();
            //批量删除
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("cb_id");

                if (cb.Checked)
                {
                    Model.attr_field model = bll.GetModel(id);
                    if (model.is_sys == 1)
                    {
                        ShowError("字段" + model.title + "是系统字段，不可删除");
                        continue;
                    }
                    if (new BLL.attr_value().ExistsByFieldId(id))
                    {
                        ShowError("字段" + model.title + "已和栏目绑定，不可删除");
                        continue;
                    }
                    //删除扩展属性
                    bll.Delete(id);
                    //删除选项
                    new BLL.attr_item().DeleteByFieldId(id);
                    //删除扩展属性值
                    //new DAL.attr_value().DeleteByFid(id);
                }
            }
            //保存日志
            SaveLogs(string.Format("[{0}管理]批量删除扩展属性", channel.manage_title));
            JscriptPrint("批量删除成功啦！", Request.RawUrl, "Success");
        }
        //保存排序
        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            chkLoginLevel("editAttrfield" + channel_id);
            He.BLL.attr_field bll = new He.BLL.attr_field();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                int sortId;
                if (!int.TryParse(((TextBox)rptList.Items[i].FindControl("txtSortId")).Text.Trim(), out sortId))
                {
                    sortId = 99;
                }
                bll.UpdateField(id, "sort_id=" + sortId.ToString());
            }
            //保存日志
            SaveLogs(string.Format("[{0}管理]批量保存排序", channel.manage_title));
            JscriptPrint("排序保存成功啦！", Request.RawUrl, "Success");
        }

    }
}